package com.hks360.car_treasure.mvp.equipment.presenter;

import android.content.Context;

import com.hks360.car_treasure.model.BaseModel;
import com.hks360.car_treasure.model.CarLocation;
import com.hks360.car_treasure.model.CarState;
import com.hks360.car_treasure.mvp.BaseModelListener;
import com.hks360.car_treasure.mvp.equipment.model.EquipmentModel;
import com.hks360.car_treasure.mvp.equipment.model.EquipmentModelImpl;
import com.hks360.car_treasure.mvp.equipment.view.EquipmentView;

/**
 * Created by xiaote on 2016/3/30.
 */
public class EquipmentPresenterImpl implements EquipmentPresenter {
    private EquipmentModel equipmentModel;
    private EquipmentView equipmentView;

    public EquipmentPresenterImpl(Context context,EquipmentView equipmentView) {
        equipmentModel = new EquipmentModelImpl(context);
        this.equipmentView = equipmentView;
    }

    @Override
    public void bindUser(int what) {
        BaseModelListener listener = new BaseModelListener() {
            @Override
            public void onSuccess(int what, BaseModel baseModel) {
                if(baseModel.getErrcode() == 0) {
                    equipmentView.showToastMsg("设备连接成功！");
                    equipmentView.bindUserSuccess();
                } else {
                    equipmentView.showToastMsg(baseModel.getErrmsg());
                }
            }

            @Override
            public void onFailed(int what) {
                equipmentView.showToastMsg("设备连接失败，请稍后重新尝试连接!");
            }

            @Override
            public void onStart(int what) {
                equipmentView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                equipmentView.hideLoading();
            }
        };
        equipmentModel.bindUser(what,equipmentView.getMobile(),equipmentView.getVin(),equipmentView.getTboxId(),listener);
    }

    @Override
    public void getCurrentState(int what) {
        EquipmentModelImpl.CarListener listener = new EquipmentModelImpl.CarListener() {
            @Override
            public void onSuccess(int what, CarState carState) {
                equipmentView.getCurrentStateSuccess(carState);
            }

            @Override
            public void onFailed(int what) {
                equipmentView.showToastMsg("获取状态失败");
            }

            @Override
            public void onStart(int what) {
//                equipmentView.showLoading();
            }

            @Override
            public void onFinish(int what) {
//                equipmentView.hideLoading();
            }
        };
        equipmentModel.getCurrentState(what,equipmentView.getTboxId(),listener);
    }

    @Override
    public void sendCommand(int what,String command) {
        BaseModelListener listener = new BaseModelListener() {
            @Override
            public void onSuccess(int what, BaseModel model) {

            }

            @Override
            public void onFailed(int what) {

            }

            @Override
            public void onStart(int what) {
                equipmentView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                equipmentView.hideLoading();
            }
        };
        equipmentModel.sendCommand(what,equipmentView.getTboxId(),command,listener);
    }

    @Override
    public void readCurrentLocation(int what) {
        EquipmentModelImpl.CarLocationListener listener  = new EquipmentModelImpl.CarLocationListener() {
            @Override
            public void onSuccess(int what, CarLocation carLocation) {
                if(carLocation != null) {
                    equipmentView.readCurrentLocationSuccess(carLocation);
                } else {
                    equipmentView.readCurrentLocationFailed();
                }
            }

            @Override
            public void onFailed(int what) {
                equipmentView.readCurrentLocationFailed();
            }

            @Override
            public void onStart(int what) {
                equipmentView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                equipmentView.hideLoading();
            }
        };
        equipmentModel.readCurrentLocation(what,equipmentView.getTboxId(),listener);
    }
}
